//
//  CollectionViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/14.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class CollectionViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBaseLayout()
        layoutPageSubviews()
        configUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    func changeTableViewAction(sender: UIButton) {
        if sender.tag == 10 {
            UIView.animate(withDuration: 0.5, animations: {(_) in
                self.lineView.frame.origin.x = 0
                self.scrollView.contentOffset.x = 0
            })
        } else {
            UIView.animate(withDuration: 0.5, animations: {(_) in
                self.lineView.frame.origin.x = SCREEN_WIDTH/2
                self.scrollView.contentOffset.x = SCREEN_WIDTH
            })
        }
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(topView)
        self.view.addSubview(productLabel)
        self.view.addSubview(brandLabel)
        self.view.addSubview(lineView)
        self.view.addSubview(scrollView)
        
    }
    
    func layoutPageSubviews() {
        topView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.top.equalTo(self.view).offset(0)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(40)
        }
        
        productLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(60)
            make.centerY.equalTo(topView)
            make.width.equalTo(70)
            make.height.equalTo(16)
        }
        
        brandLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.view).offset(-60)
            make.centerY.equalTo(topView)
            make.width.equalTo(70)
            make.height.equalTo(16)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.bottom.equalTo(topView.snp.bottom).offset(-2)
            make.width.equalTo(SCREEN_WIDTH/2)
            make.height.equalTo(2)
        }
        
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 40, left: 0, bottom: 49, right: 0))
        }
    }
    
    func configUI() {
        for i in 0 ..< 2 {
            let tableView = UITableView(frame: CGRect(x: SCREEN_WIDTH*CGFloat(i), y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-40-49), style: .plain)
            tableView.separatorStyle = .none
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(CollectionCell.self, forCellReuseIdentifier: "CollectionCell")
            tableView.tag = i
            
            let button = UIButton(type: .custom)
            button.frame = CGRect(x: SCREEN_WIDTH/2*CGFloat(i), y: 0, width: SCREEN_WIDTH/2, height: 40)
            button.backgroundColor = UIColor.clear
            button.tag = 10+i
            button.addTarget(self, action: #selector(CollectionViewController.changeTableViewAction(sender:)), for: .touchUpInside)
            
            self.topView.addSubview(button)
            self.scrollView.addSubview(tableView)
        }
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        self.title = "我的收藏"
        self.automaticallyAdjustsScrollViewInsets = false
    }
    
    func addSwipeButton(cell: MGSwipeTableCell) {
        let deleteBtn = MGSwipeButton(title: "删除", backgroundColor: UIColor(hexString: "#B50834"), callback: nil)
        deleteBtn?.frame = CGRect(x: 0, y: 0, width: 94, height: 128)
        deleteBtn?.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
        deleteBtn?.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 14)
        
        cell.rightButtons = [deleteBtn!]
    }
    
    // MARK: - setters and getters
    var _topView: UIView!
    var topView: UIView {
        if _topView == nil {
            _topView = UIView()
            _topView.backgroundColor = UIColor.white
        }
        return _topView
    }
    
    var _productLabel: UILabel!
    var productLabel: UILabel {
        if _productLabel == nil {
            _productLabel = UILabel()
            _productLabel.font = UIFont(name: "PingFang-SC-Regular", size: 14)
            _productLabel.textColor = UIColor(hexString: "#252525")
            _productLabel.textAlignment = .center
            _productLabel.text = "喜欢的商品"
        }
        return _productLabel
    }
    
    var _brandLabel: UILabel!
    var brandLabel: UILabel {
        if _brandLabel == nil {
            _brandLabel = UILabel()
            _brandLabel.font = UIFont(name: "PingFang-SC-Regular", size: 14)
            _brandLabel.textColor = UIColor(hexString: "#666666")
            _brandLabel.textAlignment = .center
            _brandLabel.text = "关注的品牌"
        }
        return _brandLabel
    }
    
    var _lineView: UIView!
    var lineView: UIView {
        if _lineView == nil {
            _lineView = UIView()
            _lineView.backgroundColor = UIColor(hexString: "#252525")
        }
        return _lineView
    }
    
    var _scrollView: UIScrollView!
    var scrollView: UIScrollView {
        if _scrollView == nil {
            _scrollView = UIScrollView()
            _scrollView.contentOffset = CGPoint(x: 0, y: 0)
            _scrollView.contentSize = CGSize(width: SCREEN_WIDTH*2, height: SCREEN_HEIGHT-104-49)
            _scrollView.isPagingEnabled = true
            _scrollView.bounces = false
            _scrollView.isDirectionalLockEnabled = true
            _scrollView.alwaysBounceVertical = false
            _scrollView.alwaysBounceHorizontal = true
            _scrollView.showsHorizontalScrollIndicator = false
            _scrollView.showsVerticalScrollIndicator = false
            _scrollView.delegate = self
            _scrollView.tag = 222
        }
        return _scrollView
    }
    


}

extension CollectionViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 0 {
            return 3
        }
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let string = "CollectionCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! CollectionCell
        cell.configUI()
        if tableView.tag == 0 {
            cell.index = 0
        } else {
            cell.index = 1
        }
        addSwipeButton(cell: cell)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }
}

extension CollectionViewController: UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset.x)
        
        if scrollView.tag == 222 {
            self.lineView.frame.origin.x = scrollView.contentOffset.x/2
        }
    }

}




